﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class RayShooter : MonoBehaviour
{

    private Camera _camera;

	// Use this for initialization
	void Start ()
	{
	    _camera = GetComponent<Camera>();
        
        //隐藏光标
//        Cursor.lockState = CursorLockMode.Locked;
//	    Cursor.visible = false;
	}
	
	// Update is called once per frame
	void Update () {
	    if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
	    {
	        Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
	        Ray ray = _camera.ScreenPointToRay(point);
	        RaycastHit hit;
	        if (Physics.Raycast(ray, out hit))
	        {
	            //Debug.Log("Hit:"+hit.point);
	            GameObject hitObject = hit.transform.gameObject;
	            ReactiveTarget target = hitObject.GetComponent<ReactiveTarget>();
	            if (target != null)
	            {
	                target.ReactToHit();
                    //响应受打击时添加的消息广播
                    Messenger.Broadcast(GameEvent.ENEMY_HIT);
	            }
	            else
	            {
	                StartCoroutine(SphereIndicator(hit.point));
                }
	        }
	    }
	}
    
    //协程
    private IEnumerator SphereIndicator(Vector3 pos)
    {
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = pos;
        yield return new  WaitForSeconds(1);

        //移除sphere
        Destroy(sphere);
    }

    void OnGUI()
    {
        int size = 12;
        float posX = _camera.pixelWidth / 2 - size / 4;
        float posY = _camera.pixelHeight / 2 - size / 2;
        GUI.Label(new Rect(posX, posY, size, size), "*");
    }
}
